//
//  CustomView.swift
//  Runner
//
//  Created by longxiaobai on 2024/8/8.
//

import Foundation
import Flutter

class CustomView: NSObject, FlutterPlatformView {
    private var _view: UIView
    let frame: CGRect
    var channel : FlutterMethodChannel!
    var messenger: FlutterBinaryMessenger!
    
    init(
        frame: CGRect,
        viewIdentifier viewId: Int64,
        arguments args: Any?,
        binaryMessenger messenger: FlutterBinaryMessenger
    ) {
        self.frame = frame
        self.messenger = messenger
        // 初始化你的原生视图
        _view = UIView(frame: frame)
        // _view.backgroundColor = UIColor.gray
        print("----------> CustomView")
        super.init()
        
        self.initMethodChannel()
        
        // 在这里可以添加更多的原生视图初始化代码
        let label:UILabel = UILabel()
        // 根据需要调整位置和大小
        label.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
        // 设置label的标题
        label.text = "initMethodChannel"
        // 设置label的文本颜色
        label.textColor = UIColor.red
        // 设置label的对齐方式
        label.textAlignment = .center
        // 设置label的阴影颜色
        label.shadowColor = UIColor.gray
        // 设置label的阴影大小
        label.shadowOffset = CGSize(width:1.5,height:1.5)
        _view.addSubview(label)
        
        let bgView = UIView()
        bgView.frame = CGRect(x: 0, y: 120, width: 200, height: 60)
        // bgView.backgroundColor = UIColor.green
        _view.addSubview(bgView)
        
        let button = UIButton(type: .system)
        // 根据需要调整位置和大小
        button.frame = CGRect(x: 0, y: 0, width: 200, height: 40)
        // 设置标题
        button.setTitle("Button", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        // 添加按钮点击事件
        button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        // 设置背景颜色
        button.backgroundColor = UIColor.red;
        // 设置按钮的圆角
        button.layer.cornerRadius = 10
        button.layer.masksToBounds = true // 确保圆角不会超出按钮的边界

        
        bgView.addSubview(button)
        _view.addSubview(bgView)
        
        let stabel:UILabel = UILabel()
        // 根据需要调整位置和大小
        stabel.frame = CGRect(x: 0, y: 40, width: 200, height: 40)
        // 设置label的标题
        stabel.text = "FlutterPlatformView"
        // 设置label的对齐方式
        stabel.textAlignment = .center
        _view.addSubview(stabel)
    }
    
    // 按钮点击事件的处理函数
    @objc func buttonTapped() {
        print("按钮被点击了")
        var args = Dictionary<String,Any>()
        args["name"] = "longxiaobai"
        args["age"] = 18
        args["height"] = 1.88
        self.channel.invokeMethod("sendMessageFromPlatform", arguments: args)
    }
    
    // 实现FlutterPlatformView的方法
    func view() -> UIView {
        return self._view
    }
    
    func initMethodChannel () {
        let channelName = "com.example/custom_channel_view"
        self.channel = FlutterMethodChannel.init(name: channelName,
                                                 binaryMessenger: messenger);
        self.channel.setMethodCallHandler({
            (call: FlutterMethodCall, result: FlutterResult) -> Void in
            if (call.method == "setPlatformView"){
                self.showAlertUI()
                print("==== setPlatformView ====")
            }else if(call.method == "startLoaction"){
                // do something
            }else if(call.method == "stopLoaction"){
                // do something
            }else{
                result(FlutterMethodNotImplemented)
            }
        });
    }
    
    func showAlertUI () {
        // 创建UIAlertController
        let alertController = UIAlertController(title: "标题", message: "可以非常灵活的定制弹框的所有UI属性", preferredStyle: .alert)
        
        // 创建按钮操作
        let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (action) in
            // 取消按钮的响应处理
        }
        
        let confirmAction = UIAlertAction(title: "确认", style: .default) { (action) in
            // 确认按钮的响应处理
        }
        
        // 将按钮操作添加到UIAlertController
        alertController.addAction(cancelAction)
        alertController.addAction(confirmAction)
        
        // 显示UIAlertController
        UIApplication.shared.windows.first?.rootViewController?.present(alertController, animated: true, completion: nil)
    }
}
